package com.javaspeak.designpatterns.go4.structural.composite;


/**
 * Composite pattern
 *
 * Has a tree like structure of simple and composite objects.  A Composite
 * object can itself hold composite objects as well as simple objects.
 * <p>
 * In this example the composite objects are modelling html tables while the
 * simple objects are modelling text.  This example is not too disimilar to the
 * apache tiles framework. The apache tile framework uses the composite
 * framework.
 *
 * @author John Dickerson
 */
public class ApplicationComposite {

    /**
     * Shows a web page made up of tables containing text and other tables.
     */
    public void showWebPage(){

        HtmlElement pageTableElement = new TableElement();

        pageTableElement.getHtmlElements().add(
                new TextElement( "Composite Pattern" ) );

        HtmlElement bodyTableElement = new TableElement();
        bodyTableElement.getHtmlElements().add( new TextElement( "Hello" ) );
        bodyTableElement.getHtmlElements().add( new TextElement( "World!" ) );
        pageTableElement.getHtmlElements().add( bodyTableElement );

        pageTableElement.getHtmlElements().add(
                new TextElement( "Footer & Copyright" ) );

        StringBuilder htmlSb = new StringBuilder();
        htmlSb.append( "<html>\n" );
        htmlSb.append( "<head><title>Composite Pattern</title></head>\n" );
        htmlSb.append( "<body>\n");
        htmlSb.append( pageTableElement.getHtml() );
        htmlSb.append( "</body>" );
        htmlSb.append( "</html>" );

        System.out.println( htmlSb.toString() );
    }


    public static void main(String[] args) {

        ApplicationComposite applicationComposite = new ApplicationComposite();
        applicationComposite.showWebPage();
    }
}
